package com.tanhua.server.service;

import cn.hutool.core.util.RandomUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VoiceApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.Voice;
import com.tanhua.model.vo.VoiceVO;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class VoiceService {
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private VoiceApi voiceApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    public VoiceVO   findVoice() {
        Long userId = UserHolder.getUserId();
        // 先判断已经获取了几条
      int count =  voiceApi.findByUserId(userId);
      // 剩余次数
      int remainingTimes = 10 -count;
      if(remainingTimes == 0){
          return null;
        }
      // 从数据库查出语音集合
       List<Voice> ice =  voiceApi.findList(userId);
      // 随机获取其中的1条
        if(ice.size() == 0){
            return new VoiceVO();
        }
        int i = RandomUtil.randomInt(0, ice.size() - 1);
        Voice voice = ice.get(i);
        voice.setReceiveId(userId);
        //String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        voice.setRecived(new Date());
        voice.setLikeType(1);
        voiceApi.update(voice);
        VoiceVO vo = new VoiceVO();
        UserInfo userInfo = userInfoApi.findById(voice.getUserId());
        vo.setId(Integer.valueOf(userInfo.getId().toString()));
        vo.setAge(userInfo.getAge());
        vo.setAvatar(userInfo.getAvatar());
        vo.setGender(userInfo.getGender());
        vo.setNickname(userInfo.getNickname());
        vo.setRemainingTimes(remainingTimes);
        vo.setSoundUrl(voice.getVoiceUrl());
        return vo;

    }

    public Boolean sendVoice(MultipartFile soundFile) throws IOException {
       Boolean flag =  voiceApi.findCount(UserHolder.getUserId());
        if(!flag){
            return false;
        }
        String filename = soundFile.getOriginalFilename();  // abc.mp4
        filename = filename.substring(filename.lastIndexOf(".") + 1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), filename, null);
        String videoUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        Voice voice = new Voice();
        voice.setUserId(UserHolder.getUserId());
        voice.setVoiceUrl(videoUrl);
        //redisTemplate.opsForValue().set("publishCount",a++);
        voice.setLikeType(0);
        voice.setCreated(new Date());
        voiceApi.sendVoice(voice);
        return true;
    }
}
